package com.qf.agro.service.sys.impl;

import com.qf.agro.mapper.RoleMapper;
import com.qf.agro.pojo.SysRole;
import com.qf.agro.service.sys.RoleService;
import io.swagger.v3.oas.annotations.servers.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 千锋健哥
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<SysRole> findListByUserId(Long userId) {
        return roleMapper.findListByUserId(userId);
    }

    @Override
    public List<SysRole> findAllRoleList() {
        List<SysRole> list = roleMapper.selectList(null);
        return list;
    }

    @Override
    public void updateRoles(Long userId, List<String> roleIds) {
        //1. 根据用户id删除用户和角色的关联关系
        roleMapper.deleteByUserId(userId);

        //2. 遍历角色列表
        if (roleIds != null) {
            for (String roleId : roleIds) {
                if (roleId == null) {
                    continue;
                }
                Long rid = null;
                try {
                    rid = Long.parseLong(roleId);
                } catch (Exception e) {
                    continue;
                }

                //3. 添加用户和角色的关联关系
                roleMapper.insertUserIdAndRoleId(userId, rid);
            }
        }


    }

}
